package org.example.day06;

/**
 * 第二章节:2.3.1.52: synchronized代码块也具有增加可见性的作用
 *      case2: ①添加synchronized解决死锁
 *             ②synchronized的作用有同步主内存和线程的私有内存
 */
public class MyThread6 {
    private boolean isContinue = true;
    public void runMethod(){
        String anything = new String(); //锁对象
        while(isContinue == true){
            synchronized (anything){

            }
        }
        System.out.println("停下来了");
    }
    public void stopMethod(){
        isContinue = false;
    }

    public static void main(String[] args) {
        try {
            MyThread6 m6 = new MyThread6();
            Thread6A ta = new Thread6A(m6);
            ta.start();
            Thread.sleep(1000);
            Thread6B tb = new Thread6B(m6);
            tb.start();
            System.out.println("以发动B去停止A线程");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class Thread6A extends Thread{
    private MyThread6 m6;

    public Thread6A(MyThread6 m6) {
        this.m6 = m6;
    }

    @Override
    public void run() {
        m6.runMethod();
    }
}

class Thread6B extends Thread{
    private MyThread6 m6;

    public Thread6B(MyThread6 m6) {
        this.m6 = m6;
    }

    @Override
    public void run() {
        m6.stopMethod();
    }
}

